import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'//home这种属于是一打开就全部加载了
import { post } from '@/api/index'
import _import from './_import'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      title: "未来社区首页 ",
      auth: true,
      authArr: [1, 2, 3],
      requireAuth: true
    },
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: "用户登录"
    },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    meta: {
      title: "用户注册"
    },
    component: () => import('../views/Register.vue')
  },
  {
    path: '/result/:type',
    name: 'Result',
    meta: {
      title: "操作结果"
    },
    component: () => import('../views/Result.vue'),
    props: true
  },
  {
    path: '/topic',
    name: 'Topic',
    meta: {
      title: "话题",
      auth: true,
      authArr: [0, 1, 2]
    },
    component: () => import('../views/Topic.vue')
  },
  {
    path: '/post',
    name: 'Post',
    meta: {
      title: "发布新话题",
      auth: true,
      authArr: [0, 1, 2]
    },
    component: () => import('../views/Post.vue')
  },
  {
    path: '/topicdetails/:id',
    name: 'Topicdetails',
    meta: {
      title: "话题详情",
      auth: true,
      authArr: [0, 1, 2]
    },
    component: () => import('../views/Topicdetails.vue'),
    props: true
  },
  {
    path: '/usercenter/:uid',
    name: 'UserCenter',
    meta: {
      title: "用户中心",
      auth: true,
      authArr: [0, 1, 2]
    },
    component: () => import('../views/UserCenter.vue'),
    props: true
  },
  {
    path: '/personaldata/',
    name: 'Personaldata',
    meta: {
      title: "个人资料",
      auth: true,
      authArr: [0, 1, 2]
    },
    component: () => import('../views/Personaldata.vue'),
    props: true
  },
  {
    path: '/personal',
    name: 'Personal',
    meta: {
      title: "用户话题"
    },
    component: () => import('../views/Personal.vue'),
    props: true
  },
]
const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.addRoute({
  path: "/topClass",
  name: 'TopClass',
  meta: {
    title: "话题分类",
    auth: true,
    authArr: [0, 1, 2]
  },
  component: _import('TopClass.vue'),
  props: true
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.meta.auth) {
    post('/isLogin.php')
      .then((res) => {
        let { ret } = res;
        if (ret === 200) {
          next()
        } else {
          next({
            name: "Login"
          })
        }
      })
    next()
  } else {
    next()
  }


})

export default router
